package com.unionftech.common.widget.ui

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.unionftech.common.CommonConstants
import com.unionftech.common.R
import com.unionftech.common.base.BaseActivity
import com.unionftech.common.utils.ActivityCollector
import com.unionftech.common.utils.LogUtils
import com.unionftech.common.utils.SpUtils
import com.unionftech.common.utils.ToastUtils
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.system.exitProcess

class ChangeDevActivity : BaseActivity() {
    private val mRvList: RecyclerView by lazy { findViewById(R.id.rv_list) }
    private val mList = mutableListOf<String>()
    private val mAdapter = ChangeDevListAdapter(mList)

    companion object {
        private const val BASE_URLS = "baseUrls"

        @JvmStatic
        fun startThis(context: Context, list: List<String>) {
            val intent = Intent(context, ChangeDevActivity::class.java)
            if (context !is Activity) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            intent.putExtra(BASE_URLS, list.toTypedArray())
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun initLayout() = R.layout.activity_change_dev
    override fun obtainIntentData(intent: Intent) {
        intent.getStringArrayExtra(BASE_URLS)?.also { arr ->
            mList.addAll(arr.toList())
            LogUtils.d("mList------->${mList}")
        }
    }

    override fun initView() {
        super.initView()
        val divider = DividerItemDecoration(mContext, LinearLayoutManager.VERTICAL)
        divider.setDrawable(createDividerDrawable())
        mRvList.addItemDecoration(divider)
        mRvList.layoutManager = LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)
        mRvList.adapter = mAdapter
    }

    private fun createDividerDrawable(): Drawable {
        val drawable = GradientDrawable()
        drawable.setSize(0, 1) // 宽度自适应，高度1px
        drawable.setColor(Color.parseColor("#E0E0E0"))
        return drawable
    }

    override fun setListener() {
        super.setListener()
        mAdapter.setOnItemClickListener { _, _, position ->
            mList.getOrNull(position)?.also {
                SpUtils.getInstance()
                    .put(CommonConstants.Key.KEY_CURRENT_BASE_URL, it.split("\n")[1])
                ToastUtils.longToast("域名切换成功,请重启App")
                lifecycleScope.launch {
                    delay(3000)
                    finishAffinity(); // 关闭所有 Activity
                    android.os.Process.killProcess(android.os.Process.myPid())
                }
            }
        }
    }

    override fun setView() {
        super.setView()
        setActivityTitle("切换环境")
        mRootView.setTitleColor(mContext.getColor(R.color.color_1C1D21))
    }
}